package com.jinghangzz.ssm.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jinghangzz.ssm.common.util.ConstatFinalUtil;
import com.jinghangzz.ssm.common.util.DateUtil;
import com.jinghangzz.ssm.common.util.PageInfoUtil;
import com.jinghangzz.ssm.demo.pojo.ADemoDynasty;
import com.jinghangzz.ssm.demo.pojo.ADemoKing;
import com.jinghangzz.ssm.demo.service.IDemoDbService;

/**
 * 前台(示例模块的Controller)
 * @author TeaBig
 */
@Controller
@RequestMapping("/head/demo/")
public class DemoHeadController
{
	@Resource
	private IDemoDbService demoDbService;
	@Resource
	private DateUtil dateUtil;
	
	/**
	 * 朝代列表
	 * 		Controller要调用Service;只需要把Junit中的代码复制到Controller就可以;
			   把在Junit代码中写死的部分;从jsp要参数;
	 * @return
	 */
	@RequestMapping("/dynastyList")
	public String dynastyList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("=====dynastyList=====");
		/*=====处理分页开始=====*/
		/* 分页参数 */
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		try
		{
			String currentPage = request.getParameter("currentPage");
			String pageSize = request.getParameter("pageSize");
			
			/* 把字符串转换成整数,容易出错; */
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (Exception e)
		{
		}
		/* 存储到request中 */
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		/*=====处理分页结束=====*/
		/*=====处理搜索开始=====*/
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 搜索 */
		/* 按照关键字来搜索 */
		String keyword = request.getParameter("keyword");
		condMap.put("keyword", keyword);
		/* 存储到request中 */
		request.setAttribute("keyword", keyword);
		
		/* 按照状态搜索 */
		String status = request.getParameter("status");
		condMap.put("status", status);
		/* 存储到request中 */
		request.setAttribute("status", status);
		/* 日历 */
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		if(st == null)
		{
			st = ""; 
		}
		if(ed == null)
		{
			ed = ""; 
		}
		/* 变成Date类型 */
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateUtil.parseStr(st);
			edDate = this.dateUtil.parseStr(ed);
		}
		/* 如何把字符串变成Date */
		/* 按照时间搜索 */
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
		/* 存储到request中 */
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		/*=====处理搜索结束=====*/
		
		/* 查询一条记录 */
		List<ADemoDynasty> demoDynastyList = this.demoDbService.findCondListDynastyService(pageInfoUtil,
				condMap);
		/* 存储到request中 */
		request.setAttribute("demoDynastyList", demoDynastyList);
		/* 前缀+后缀 */
		return "/head/demo/dynastyList";
	}
	
	/**
	 * 打开添加朝代的页面
	 * @return
	 */
	@RequestMapping("/dynastyInsert")
	public String dynastyInsert()
	{
		ConstatFinalUtil.LOGGER.info("=====dynastyInsert=====");
		/* 前缀+后缀 */
		return "/head/demo/dynastyInsert";
	}
	
	/**
	 * 打开添加朝代的页面
	 * @return
	 */
	@RequestMapping("/dynastyInsertSubmit")
	public String dynastyInsertSubmit(ADemoDynasty demoDynasty,String pubTimeStr, Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====dynastyInsertSubmit=====");
		demoDynasty.setGuoZuo( demoDynasty.getEdYear() - demoDynasty.getStYear() );
		/* 发布时间 */
		Date pubTime = this.dateUtil.parseStr(pubTimeStr);
		demoDynasty.setPubTime(pubTime);
		
		demoDynasty.setCreateTime(new Date());
		demoDynasty.setUpdateTime(new Date());
		/* 调用Service,保存数据库 */
		int res = this.demoDbService.saveOneDynastyService(demoDynasty);
		model.addAttribute("demoDynasty", demoDynasty);
		if(res > 0 )
		{
			/* 成功 */
			model.addAttribute("info", "成功");
		}else
		{
			/* 失败 */
			model.addAttribute("info", "失败");
		}
		/* 前缀+后缀 */
		return this.dynastyInsert();
	}
	
	/**
	 * 打开更新的页面
	 * 	根据Id查询记录
	 * @return
	 */
	@RequestMapping("/dynastyUpdate")
	public String dynastyUpdate(String id,Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====dynastyUpdate=====");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 按照id搜索 */
		condMap.put("id", id);
		/* 查询一条记录 */
		ADemoDynasty demoDynasty = this.demoDbService.findOneDynastyService(condMap);
		model.addAttribute("demoDynasty", demoDynasty);
		/* 前缀+后缀 */
		return "/head/demo/dynastyUpdate";
	}
	
	/**
	 * 打开更新(提交操作)
	 * 		驱动赋值要求:
	 * 			只有在请求体中的键与pojo的属性一样时,才会自动赋值;否则的话是不会自动的赋值的
	 * @return
	 */
	@RequestMapping("/dynastyUpdateSubmit")
	public String dynastyUpdateSubmit(String id,String pubTimeStr,
			HttpServletRequest request,
			Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====dynastyUpdateSubmit=====");
		/* 从数据库中根据id查询一次,然后再一一的修改值 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 按照id搜索 */
		condMap.put("id", id);
		/* 查询一条记录 */
		ADemoDynasty demoDynasty = this.demoDbService.findOneDynastyService(condMap);
		model.addAttribute("demoDynasty", demoDynasty);
		
		
		/* 只能一个一个的接收页面上的参数(不是数据库里面的全部字段)
		 * 一个一个的处理 */
		String name = request.getParameter("name");
		String stYear = request.getParameter("stYear");
		String edYear = request.getParameter("edYear");
		String capital = request.getParameter("capital");
		String status = request.getParameter("status");
		String content = request.getParameter("content");
		demoDynasty.setName(name);
		demoDynasty.setStYear(Integer.valueOf(stYear));
		demoDynasty.setEdYear(Integer.valueOf(edYear));
		demoDynasty.setCapital(capital);
		demoDynasty.setStatus(Byte.valueOf(status));
		demoDynasty.setContent(content);
		
		demoDynasty.setUpdateTime(new Date());
		
		int res = this.demoDbService.updateOneDynastyService(demoDynasty);
		if(res > 0 )
		{
			/* 成功 */
			model.addAttribute("info", "成功");
		}else
		{
			/* 失败 */
			model.addAttribute("info", "失败");
		}
		/* 前缀+后缀 */
		return this.dynastyUpdate(id,model);
	}
	
	/**
	 * 皇上列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/kingList")
	public String kingList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("=====kingList=====");
		return "/head/demo/kingList";
	}
	
	/**
	 * 打开添加皇上的页面
	 * @return
	 */
	@RequestMapping("/kingInsert")
	public String kingInsert(Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====kingInsert=====");
		/* 查询一下状态为启用的朝代 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 搜索 */
		/* 按照状态搜索 */
		condMap.put("status", "1");
		/* 查询一条记录 */
		List<ADemoDynasty> demoDynastyList = this.demoDbService.findCondListDynastyService(null,
				condMap);
		model.addAttribute("demoDynastyList", demoDynastyList);
		/* 前缀+后缀 */
		return "/head/demo/kingInsert";
	}
	
	/**
	 * 打开添加皇上的页面
	 * @return
	 */
	@RequestMapping("/kingInsertSubmit")
	public String kingInsertSubmit(ADemoKing demoKing,String pubTimeStr, Model model)
	{
		ConstatFinalUtil.LOGGER.info("=====kingInsertSubmit=====");
		/* 发布时间 */
		Date pubTime = this.dateUtil.parseStr(pubTimeStr);
		demoKing.setPubTime(pubTime);
		
		demoKing.setCreateTime(new Date());
		demoKing.setUpdateTime(new Date());
		/* 调用Service,保存数据库 */
		int res = this.demoDbService.saveOneKingService(demoKing);
		model.addAttribute("demoKing", demoKing);
		if(res > 0 )
		{
			/* 成功 */
			model.addAttribute("info", "成功");
		}else
		{
			/* 失败 */
			model.addAttribute("info", "失败");
		}
		return this.kingInsert(model); 
	}
}
